package com.xxx.yuedong.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.xxx.yuedong.common.consts.MessageTemplates;
import com.xxx.yuedong.common.consts.ObjectTypeConstants;
import com.xxx.yuedong.common.consts.QueueConstants;
import com.xxx.yuedong.community.controller.form.CommentDetailForm;
import com.xxx.yuedong.community.pojo.Remind;
import com.xxx.yuedong.community.service.*;
import com.xxx.yuedong.mine.pojo.vo.BeFollowVO;
import com.xxx.yuedong.mine.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author HG
 * @version 1.0
 **/
@Component
@Slf4j
public class EventListener {
    @Autowired
    private RemindService remindService;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private UserService userService;

    @Autowired
    private FollowService followService;

    @Autowired
    private AnnounceService announceService;

    @Autowired
    private CommentService commentService;

    /**
     * 监听点赞事件
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.LIKE)
    public void process1(JSONObject message) {
        //给被点赞的用户新增提醒记录
        Long userId = (Long) message.get("userId");
        String event = (String) message.get("event");
        Long dynamicId = (Long) message.get("objectId");

        HashMap dynamicDetail = dynamicService.searchDynamicDetailById(dynamicId);
        String nickname = (String) dynamicDetail.get("nickName");
        String content = (String) dynamicDetail.get("content");

        Long recipientId = (Long) dynamicDetail.get("userId");
        String msg = nickname + MessageTemplates.LIKED_TEMPLATE;
        Date createTime = new Date();
        Byte isRead = 0;

        Remind remind = new Remind();
        remind.setSenderId(userId);
        remind.setSenderName(nickname);
        remind.setSenderAction(event);
        remind.setObjectId(dynamicId);
        remind.setObjectType(ObjectTypeConstants.DYNAMIC);
        remind.setObjectContent(content);
        remind.setRecipientId(recipientId);
        remind.setMessage(msg);
        remind.setCreateTime(createTime);
        remind.setIsRead(isRead);

        remindService.insert(remind);
    }

    /**
     * 监听更新事件
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.UPDATE)
    public void process2(JSONObject message) {
        //关注动态更新时新增提醒记录
        Long userId = (Long) message.get("userId");
        List<BeFollowVO> beFollowList = followService.queryBeFollowList(userId);

        String event = (String) message.get("event");
        Long dynamicId = (Long) message.get("objectId");

        HashMap userInfo = userService.searchUserInfo(userId);
        String nickname = (String) userInfo.get("nickName");

        HashMap dynamic = dynamicService.searchDynamicDetailById(dynamicId);
        String content = (String) dynamic.get("content");

        String msg = nickname + ":" + MessageTemplates.UPDATE_TEMPLATE;
        Date createTime = new Date();
        Byte isRead = 0;
        for (BeFollowVO beFollowVO : beFollowList) {
            Long recipientId = beFollowVO.getUserId();
            Remind remind = new Remind();
            remind.setSenderId(userId);
            remind.setSenderName(nickname);
            remind.setSenderAction(event);
            remind.setObjectId(dynamicId);
            remind.setObjectType(ObjectTypeConstants.DYNAMIC);
            remind.setObjectContent(content);
            remind.setRecipientId(recipientId);
            remind.setMessage(msg);
            remind.setCreateTime(createTime);
            remind.setIsRead(isRead);

            remindService.insert(remind);
        }
    }

    /**
     * 监听关注事件
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.FOLLOW)
    public void process3(JSONObject message) {
        //给被关注的用户新增提醒记录
        Long userId1 = (Long) message.get("userId");//关注者
        String event = (String) message.get("event");
        Long userId2 = (Long) message.get("objectId");//被关注者

        HashMap userInfo = userService.searchUserInfo(userId1);
        String nickname = (String) userInfo.get("nickName");

        Long recipientId = userId2;
        String msg = nickname + MessageTemplates.FOLLOWED_TEMPLATE;
        Date createTime = new Date();
        Byte isRead = 0;

        Remind remind = new Remind();
        remind.setSenderId(userId1);
        remind.setSenderName(nickname);
        remind.setSenderAction(event);
        remind.setObjectId(userId2);
        remind.setObjectType(ObjectTypeConstants.USER);
        remind.setRecipientId(recipientId);
        remind.setMessage(msg);
        remind.setCreateTime(createTime);
        remind.setIsRead(isRead);

        remindService.insert(remind);
    }

    /**
     * 监听评论事件
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.COMMENT)
    public void process4(JSONObject message) {
        //给被评论的用户新增提醒记录
        Long userId = (Long) message.get("userId");
        String event = (String) message.get("event");
        Long commentId = (Long) message.get("objectId");

        HashMap userInfo = userService.searchUserInfo(userId);
        String nickname = (String) userInfo.get("nickName");

        CommentDetailForm comment = commentService.getCommentById(commentId);
        String commentContent = comment.getContent();

        HashMap dynamic = dynamicService.searchDynamicDetailById(comment.getDynamicId());
        Long recipientId = (Long) dynamic.get("userId");

        String msg = nickname + MessageTemplates.COMMENT_TEMPLATE2 + commentContent;
        Date createTime = new Date();
        Byte isRead = 0;

        Remind remind = new Remind();
        remind.setSenderId(userId);
        remind.setSenderName(nickname);
        remind.setSenderAction(event);
        remind.setObjectId(commentId);
        remind.setObjectType(ObjectTypeConstants.COMMENT);
        remind.setObjectContent(commentContent);
        remind.setRecipientId(recipientId);
        remind.setMessage(msg);
        remind.setCreateTime(createTime);
        remind.setIsRead(isRead);

        remindService.insert(remind);
    }

    /**
     * 监听评论事件
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.COMMENTED_RESPONSE)
    public void process5(JSONObject message) {
        //给被评论的用户新增提醒记录
        Long userId = (Long) message.get("userId");
        String event = (String) message.get("event");
        Long parentCommentId = (Long) message.get("objectId");

        HashMap userInfo = userService.searchUserInfo(userId);
        String nickname = (String) userInfo.get("nickName");

        CommentDetailForm comment = commentService.getCommentById(parentCommentId);
        String parentCommentContent = comment.getContent();

        /*
        HashMap subComment = commentService.getSubCommentByParentIdAndUserId(parentCommentId, userId);
        String subCommentContent = (String) subComment.get("content");*/

        Long recipientId = comment.getUserId();

        String msg = nickname + MessageTemplates.COMMENT_TEMPLATE1;
        Date createTime = new Date();
        Byte isRead = 0;

        Remind remind = new Remind();
        remind.setSenderId(userId);
        remind.setSenderName(nickname);
        remind.setSenderAction(event);
        remind.setObjectId(parentCommentId);
        remind.setObjectType(ObjectTypeConstants.COMMENT);
        remind.setObjectContent(parentCommentContent);
        remind.setRecipientId(recipientId);
        remind.setMessage(msg);
        remind.setCreateTime(createTime);
        remind.setIsRead(isRead);

        remindService.insert(remind);
    }

    /**
     * 监听公告
     *
     * @param message
     */
    @RabbitHandler
    @RabbitListener(queues = QueueConstants.ANNOUNCE)
    public void process6(JSONObject message) {
        //关注公告更新时新增提醒记录
        Long userId = (Long) message.get("userId");
        List<HashMap> allUserInfo = userService.searchAllUserInfo();

        String event = (String) message.get("event");
        Long announceId = (Long) message.get("objectId");

        HashMap userInfo = userService.searchUserInfo(userId);
        String nickname = (String) userInfo.get("nickName");

        HashMap announceInfo = announceService.searchAnnounceById(announceId);
        String content = (String) announceInfo.get("content");

        String msg = MessageTemplates.ANNOUNCE_TEMPLATE;
        Date createTime = new Date();
        Byte isRead = 0;
        for (HashMap user : allUserInfo) {
            Long recipientId = (Long) user.get("id");
            Remind remind = new Remind();
            remind.setSenderId(userId);
            remind.setSenderName(nickname);
            remind.setSenderAction(event);
            remind.setObjectId(announceId);
            remind.setObjectType(ObjectTypeConstants.ANNOUNCE);
            remind.setObjectContent(content);
            remind.setRecipientId(recipientId);
            remind.setMessage(msg);
            remind.setCreateTime(createTime);
            remind.setIsRead(isRead);

            remindService.insert(remind);
        }
    }
}
